<?php

namespace App\Commons\Libs\KafkaPHP;

use App\Commons\Libs\KafkaPHP\Commands\KafkaConsumerCommand;
use App\Commons\Libs\KafkaPHP\KafkaConfig;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;

class KafkaServiceProvider extends ServiceProvider implements DeferrableProvider
{
    public function boot()
    {
        if ($this->app->runningInConsole()) {
            $this->commands([
                KafkaConsumerCommand::class
            ]);
        }
    }
    public function register()
    {
        // $this->mergeConfigFrom(__DIR__.'/config.php', static::$abstract);

        $this->app->singleton('kafka', function ($app) {
            return new Kafka;
        });

        $this->app->singleton('kafka.producer', function ($app) {
            return new Producer($app['config']->get('kafka'));
        });

        $this->app->singleton('kafka.consumer', function ($app) {
            return new Consumer($app['config']->get('kafka'));
        });
    }

    /**
     * 取得提供者提供的服务
     *
     * @return array
     */
    public function provides()
    {
        return ['kafka', 'kafka.producer', 'kafka.consumer'];
    }
}
